package my.georings;

import com.google.android.maps.GeoPoint;

import my.georings.R;
import my.georings.data.MyLocationsManager;
import my.georings.data.MyLocation;
import my.georings.expandable.LocationEditionAbstractActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class AddActivity extends LocationEditionAbstractActivity implements OnClickListener{
	
	private static int radius[] = {100, 200, 300};

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        initSpinner();
    }

	
	@Override
	public void onClick(View v) {
		
		super.onClick(v);
		
		switch(v.getId()){
		
		///// map
		case R.id.MapButton:
			Intent myIntent = new Intent(this, my.georings.dialog.ChooseLocationMapDialog.class);
			startActivityForResult(myIntent, R.id.mapget_buttons);
			break;
			
			
		case R.id.ButtonClear:
			setAllViewsToDefault();
			currentLocation.clear();
			break;
		}
	}
	
	@Override
	protected void setListeners(){
		super.setParentListeners(this);
		findViewById(R.id.MapButton).setOnClickListener(this);
		findViewById(R.id.ButtonClear).setOnClickListener(this);
	}

	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == R.id.mapget_buttons && resultCode == RESULT_OK){
			int latitude = data.getIntExtra("lat", -1);
			int longitude = data.getIntExtra("long", -1);
			currentLocation.setPoint( new GeoPoint(latitude, longitude));
			((TextView)findViewById(R.id.TextViewLocationCurrent)).setText(
					"latitude: "+(float)latitude/1000000+"\nlongitude: " + (float)longitude/1000000);
		}
	}

	@Override
	protected void saveChanges() {
		currentLocation.setName(((EditText)findViewById(R.id.EditTextName)).getText().toString());
		int index = ((Spinner) findViewById(R.id.SpinnerRadius)).getSelectedItemPosition();
		currentLocation.setRadius(radius[index]);
		
		// wszystko jest ok
		if(currentLocation.isValid(true)){
			
			// ustawiamy id
			currentLocation.setId(MyLocationsManager.getLocationId());
			
			// i dodajemy
			if(((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().addLocation(currentLocation)){
				Toast toast = Toast.makeText(this, "New location was added", Toast.LENGTH_SHORT);
				toast.show();
				setAllViewsToDefault();
				currentLocation = new MyLocation();
			}else{
				Toast toast = Toast.makeText(this, "The location cannot be added", Toast.LENGTH_SHORT);
				toast.show();
			}
		}else{
			Toast toast = Toast.makeText(this, "The location cannot be added - fill all the necessary information", Toast.LENGTH_LONG);
			toast.show();
		}
	}

	@Override
	protected void setAllViewsToDefault() {
		((TextView)findViewById(R.id.TextViewLocationCurrent)).setText("(none)");
		((TextView)findViewById(R.id.TextViewRingCurrent)).setText("(none)");
		((TextView)findViewById(R.id.TextViewVolumeCurrent)).setText("(default)");
		
		((EditText)findViewById(R.id.EditTextName)).setText("");
		((ToggleButton)findViewById(R.id.ToggleButtonRing)).setChecked(false);
		((ToggleButton)findViewById(R.id.ToggleButtonVibra)).setChecked(false);
		((ToggleButton)findViewById(R.id.ToggleButtonVolume)).setChecked(false);
		((ToggleButton)findViewById(R.id.ToggleButtonMute)).setChecked(false);
		
		 ((Spinner) findViewById(R.id.SpinnerRadius)).setSelection(0);
		
		changeViewsForMute(false);
	}

	@Override
	protected void setContentView() {
		setContentView(R.layout.add);
	}

	@Override
	protected void setCurrentLocation() {
		currentLocation = new MyLocation();
	}
	
	private void initSpinner(){
		Spinner spinner = (Spinner) findViewById(R.id.SpinnerRadius);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.radius, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setSelection(0);
	}

}